Skip to content

Add game-of-life exercice#1007

Open
resu-xuniL wants to merge 3 commits into
exercism:mainfrom
resu-xuniL:game-of-life
Open

Add game-of-life exercice#1007
resu-xuniL wants to merge 3 commits into
exercism:mainfrom
resu-xuniL:game-of-life

Conversation

@resu-xuniL

Copy link
Copy Markdown
Contributor

Another exercice for validation.

@mk-mxp mk-mxp added x:action/create Work on something from scratch x:knowledge/intermediate Quite a bit of Exercism knowledge required x:module/practice-exercise Work on Practice Exercises x:type/content Work on content (e.g. exercises, concepts) x:size/medium Medium amount of work x:rep/medium Medium amount of reputation labels Jun 29, 2026

@mk-mxp mk-mxp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great contribution. Thanks!

Comment thread config.json
"uuid": "33d09051-b335-453b-80c1-0909a5c05b8a",
"practices": [],
"prerequisites": [],
"difficulty": 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a 4. It's looping over a nested array with basic operations and some state tracking.


declare(strict_types=1);

function tick(array $matrix): array

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again, I would want another interface: the Game of Life is a state changing exercise using ticks to modify some world state. That's what objects are for. So please make it a class.

To add some "extra spice", suggest to the student in a comment to use Asymmetric Property Visibility. The example.php cannot use this, as it has to support PHP8.2, too.

So the interface may look like this (with the usual exceptions):

class GameOfLife
{
    /**
     * In PHP 8.4 and newer you can use Asymmetric Property Visibility to enhance data encapsulation
     * @see https://www.php.net/manual/en/language.oop5.visibility.php#language.oop5.visibility-members-aviz
     */
    public function __construct(public array $matrix);
    public function tick(): void;
}

And the tests could look like:

        $matrix = [
            [0, 0, 0],
            [0, 1, 0],
            [0, 0, 0]
        ];
        $expected = [
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0]
        ];
        $subject = new GameOfLife($matrix);

        $subject->tick();

        $this->assertEquals($expected, $subject->matrix);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

x:action/create Work on something from scratch x:knowledge/intermediate Quite a bit of Exercism knowledge required x:module/practice-exercise Work on Practice Exercises x:rep/medium Medium amount of reputation x:size/medium Medium amount of work x:type/content Work on content (e.g. exercises, concepts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants